<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.jobrecords_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template  slot="menuLeft">
        <el-button type="warning" icon="el-icon-back" size="small" @click.stop="goBack()">返回列表</el-button>
      </template>
<!--      <template slot-scope="{size}" slot="menuForm">-->
<!--        <el-button type="warning" :size="size" @click="$refs.form.submit()">AI批改</el-button>-->
<!--      </template>-->
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, add, update, remove} from "@/api/service/jobrecords";
  import {mapGetters} from "vuex";

  export default {
    props: {
      jobId: {
        type: String,
        default: '0'
      },
      showMainView: {
        type: Function,
        default: null
      }
    },
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          editBtnText:'批阅',
          column: [
            {
              label: "主键",
              prop: "id",
              rules: [{
                required: true,
                message: "请输入主键",
                trigger: "blur"
              }],
              hide:true,
              display:false,
            },
            {
              label: "学生",
              prop: "studentName",
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入学生ID",
                trigger: "blur"
              }],
              editDisabled:true
            },
            {
              label: "时间",
              prop: "recordTime",
              type: "datetime",
              format: "yyyy-MM-dd HH:mm:ss",
              valueFormat: "yyyy-MM-dd HH:mm:ss",
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入时间",
                trigger: "blur"
              }],
              editDisabled:true
            },
            // {
            //   label: "地点",
            //   prop: "recordAddr",
            //   span:24,
            //   rules: [{
            //     required: true,
            //     message: "请输入地点",
            //     trigger: "blur"
            //   }],
            //   hide: true,
            //   editDisabled:true
            // },
            // {
            //   label: "描述",
            //   prop: "recordDesc",
            //   type: "textarea",
            //   span: 24,
            //   minRows: 6,
            //   rules: [{
            //     required: true,
            //     message: "请输入描述",
            //     trigger: "blur"
            //   }],
            //   hide: true,
            //   editDisabled:true
            // },
            {
              label: "完成时长",
              prop: "finishedTime",
              overHidden:true,
              span:24,
              rules: [{
                required: true,
                message: "请输入完成时长",
                trigger: "blur"
              }],
              data:'number',
              editDisabled:true
            },
            // {
            //   label: "是否公开",
            //   prop: "isPublic",
            //   rules: [{
            //     required: true,
            //     message: "请输入是否公开",
            //     trigger: "blur"
            //   }]
            // },
            {
              label: "作业附件1",
              prop: "pic1",
              rules: [{
                required: true,
                message: "请输入图1",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              accept: "image/*",
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              action: '/api/localfiles/uploadPcFile',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true,
              editDisabled:true
            },
            {
              label: "作业附件2",
              prop: "pic2",
              rules: [{
                required: false,
                message: "请输入图1",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              accept: "image/*",
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              action: '/api/localfiles/uploadPcFile',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true,
              editDisabled:true
            },
            {
              label: "作业附件3",
              prop: "pic3",
              rules: [{
                required: false,
                message: "请输入图1",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              accept: "image/*",
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              action: '/api/localfiles/uploadPcFile',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true,
              editDisabled:true
            },
            {
              label: "作业附件4",
              prop: "pic4",
              rules: [{
                required: false,
                message: "请输入图1",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              accept: "image/*",
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              action: '/api/localfiles/uploadPcFile',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true,
              editDisabled:true
            },
            {
              label: "作业附件5",
              prop: "pic5",
              rules: [{
                required: false,
                message: "请输入图1",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              accept: "image/*",
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              action: '/api/localfiles/uploadPcFile',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true,
              editDisabled:true
            },
            {
              label: "作业附件6",
              prop: "pic6",
              rules: [{
                required: false,
                message: "请输入图1",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              accept: "image/*",
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              action: '/api/localfiles/uploadPcFile',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true,
              editDisabled:true
            },
            {
              label: "教师",
              prop: "teacherName",
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入教师ID",
                trigger: "blur"
              }],
              // dicUrl: "/api/school-service/js/select",
              // props: {
              //   label: "jsName",
              //   value: "id"
              // },
              editDisabled:true
            },
            {
              label: "教师评语",
              prop: "teacherAnswer",
              type: "textarea",
              span: 24,
              minRows: 6,
              rules: [{
                required: true,
                message: "请输入教师评语",
                trigger: "blur"
              }],
              hide: true,
              maxlength:200,
              showWordLimit:true
            },
            {
              label: "教师评分",
              prop: "teacherScore",
              span:24,
              rules: [{
                required: true,
                message: "请输入教师评分",
                trigger: "blur"
              }],
              type:"number"
            },
            {
              label: "批阅1",
              prop: "correct_pic1",
              rules: [{
                required: false,
                message: "请输入批阅1",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              accept: "image/*",
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              action: '/api/localfiles/uploadPcFile',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true
            },
            {
              label: "批阅2",
              prop: "correct_pic2",
              rules: [{
                required: false,
                message: "请输入批阅1",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              accept: "image/*",
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              action: '/api/localfiles/uploadPcFile',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true
            },
            {
              label: "批阅3",
              prop: "correct_pic3",
              rules: [{
                required: false,
                message: "请输入批阅1",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              accept: "image/*",
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              action: '/api/localfiles/uploadPcFile',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true
            },
            {
              label: "批阅4",
              prop: "correct_pic4",
              rules: [{
                required: false,
                message: "请输入批阅1",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              accept: "image/*",
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              action: '/api/localfiles/uploadPcFile',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true
            },
            {
              label: "批阅5",
              prop: "correct_pic5",
              rules: [{
                required: false,
                message: "请输入批阅1",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              accept: "image/*",
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              action: '/api/localfiles/uploadPcFile',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true
            },
            {
              label: "批阅6",
              prop: "correct_pic6",
              rules: [{
                required: false,
                message: "请输入批阅1",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              accept: "image/*",
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              action: '/api/localfiles/uploadPcFile',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true
            },
            {
              label: "评价标签",
              prop: "evaluate_tag",
              overHidden:true,
              span: 24,
              rules: [{
                required: false,
                message: "请输入评价标签",
                trigger: "blur"
              }],
              type: 'select',
              editDisabled:true,
              multiple:true,
              button:true,
              dicData:[{
                label:'内容有趣',
                value:'内容有趣'

              },{
                label:'有针对性',
                value:'有针对性'
              },{
                label:'有新意',
                value:'有新意'
              },{
                label:'态度亲和',
                value:'态度亲和'
              },{
                label:'有学习性',
                value:'有学习性'
              },{
                label:'建议中肯',
                value:'建议中肯'
              }]
            },
            {
              label: "完成时长",
              prop: "evaluate1",
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入完成时长",
                trigger: "blur"
              }],
              type:"rate",
              editDisabled:true
            },
            {
              label: "难易程度",
              prop: "evaluate2",
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入难易程度",
                trigger: "blur"
              }],
              type:"rate",
              editDisabled:true
            },
            {
              label: "总体评价",
              prop: "evaluate3",
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入总体评价",
                trigger: "blur"
              }],
              type:"rate",
              editDisabled:true
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.jobrecords_add, false),
          viewBtn: true,
          delBtn: this.vaildData(this.permission.jobrecords_delete, false),
          editBtn: true
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      goBack(){
        this.showMainView();
      },
      rowSave(row, done, loading) {
        row.jobId = this.jobId;

        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        row.jobId = this.jobId;

        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          // getDetail(this.form.id).then(res => {
          //   this.form = res.data.data;
          // });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        params.id = this.jobId;

        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
